package com.imyours.webservice.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

/**
 * 
 * @author Administrator
 * 
 */
public class Context {

	/**
	 * 参数列表
	 */
	private Map<String, String> parameter = null;

	/**
	 * 结果列表
	 */
	private Map<String, Object> result = null;

	/**
	 * 执行结果状态
	 */
	private ExecuteStateEnum state = ExecuteStateEnum.fail;

	/**
	 * 错误具体提示
	 */
	private List<ServiceError> errorList = null;

	/**
	 * session对象
	 */
	private HttpSession session = null;

	public Context() {
		parameter = new HashMap<String, String>();
		result = new HashMap<String, Object>();
		errorList = new ArrayList<ServiceError>();

	}

	/**
	 * 出错时将结果状态置为错误
	 */
	public void fail(int code, String msg) {
		this.state = ExecuteStateEnum.fail;
		this.errorList.add(new ServiceError(code, msg));
	}

	/**
	 * 执行出错
	 * 
	 * @param error
	 */
	public void fail(ServiceError error) {
		this.state = ExecuteStateEnum.fail;
		this.errorList.add(error);
	}

	/**
	 * 执行出错
	 * 
	 */
	public void fail() {
		this.state = ExecuteStateEnum.fail;
	}

	/**
	 * 执行成功
	 */
	public void success() {
		this.state = ExecuteStateEnum.success;
	}

	/**
	 * 返回执行结果
	 * 
	 * @return
	 */
	public ExecuteStateEnum getState() {
		return this.state;
	}

	public void putResult(String key, Object val) {
		if (this.result == null) {
			result = new HashMap<String, Object>();
		}
		result.put(key, val);
	}

	/**
	 * 返回错误列表
	 * 
	 * @return
	 */
	public List<ServiceError> getErrors() {
		return this.errorList;
	}

	public Map<String, String> getParameter() {
		return parameter;
	}

	public void setParameter(Map<String, String> parameter) {
		this.parameter = parameter;
	}

	public Map<String, Object> getResult() {
		return result;
	}

	public void setResult(Map<String, Object> result) {
		this.result = result;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

}
